<?php

class OpinionMapper extends AbstractMapper
{

    protected function _init()
    {
	$this->setModel(new OpinionModel());
	$this->setTableName(OPINION_TABLE);
    }

    public function getOpinionList($params = array())
    {
	return $this->getAllWithRelation(array_merge(array(
			    'select' => array('o.*'),
			    'from' => 'opinion AS o',
			    'join' => 'INNER JOIN user on user.id = o.user_id',
			    'where' => 'o.user_rating >= 3',
				), $params), array('user'));
    }

    public function getOpinionCount(array $options)
    {
	return $this->getCount(array_merge(array(
			    'select' => array('COUNT(id)'),
			    'from' => 'opinion',
				), $options));
    }

    public function getByReviewId($reviewId)
    {
	return $this->getAllWithRelation(array(
		    'select' => array('o.*'),
		    'from' => 'opinion AS o',
		    'join' => 'INNER JOIN user on user.id = o.user_id',
		    'where' => 'o.target_type = \'review\' AND o.target_id = :target_id',
		    'params' => array(':target_id' => $reviewId)
			), array('user'));
    }

    public function getByCompanyId($compantId)
    {
	return $this->getAllWithRelation(array(
		    'select' => array('o.*'),
		    'from' => 'opinion AS o',
		    'join' => 'INNER JOIN user on user.id = o.user_id',
		    'where' => 'o.target_type = \'company\' AND o.target_id = :target_id',
		    'params' => array(':target_id' => $compantId)
			), array('user'));
    }
    public function getByUserId($userId, $page = 1, $limit = 10)
    {
	return $this->getAll(array(
	    'where' => 'user_id = :user_id',
	    'params' => array(':user_id' => $userId),
	    'order' => 'id DESC',
	    'offset' => ($page - 1) * $limit,
	    'limit' => $limit
	));
    }

}
